package cn.zhfeat.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;





public class CouponNumberUtil {

	/**
	 * 邀请码生成器，算法原理：<br/>
	 * 1) 获取id: 1127738 <br/>
	 * 2) 使用自定义进制转为：gpm6 <br/>
	 * 3) 转为字符串，并在后面加'o'字符：gpm6o <br/>
	 * 4）在后面随机产生若干个随机数字字符：gpm6o7 <br/>
	 * 转为自定义进制后就不会出现o这个字符，然后在后面加个'o'，这样就能确定唯一性。最后在后面产生一些随机字符进行补全。<br/>
	 */

	/** 自定义进制(0,1没有加入,容易与o,l混淆) */
	private static final char[] r = new char[] { '8', '2', '0', '1', '9', '7',
			'5', '3', '4', '6', };

	/** (不能与自定义进制有重复) */
	private static final char b = '0';

	/** 进制长度 */
	private static final int binLen = r.length;

	/** 序列最小长度 */
	private static final int s = 12;

	/**
	 * 根据ID生成12位随机码
	 * 
	 * @param id
	 *            ID
	 * @return 随机码
	 */
	public static String toSerialCode(long id) {
		char[] buf = new char[32];
		int charPos = 32;

		while ((id / binLen) > 0) {
			int ind = (int) (id % binLen);
			// System.out.println(num + "-->" + ind);
			buf[--charPos] = r[ind];
			id /= binLen;
		}
		buf[--charPos] = r[(int) (id % binLen)];
		String str = new String(buf, charPos, (32 - charPos));
		// 不够长度的自动随机补全
		if (str.length() < s) {
			StringBuilder sb = new StringBuilder();
			sb.append(b);
			Random rnd = new Random();
			for (int i = 1; i < s - str.length(); i++) {
				sb.append(r[rnd.nextInt(binLen)]);
			}
			str += sb.toString();
		}
		return str;
	}
	
	/**
	 * 根据ID生成s位随机码
	 * @return 随机码
	 */
	public static String toSerialCode(long id, int s) {
		char[] buf = new char[32];
		int charPos = 32;

		while ((id / binLen) > 0) {
			int ind = (int) (id % binLen);
			// System.out.println(num + "-->" + ind);
			buf[--charPos] = r[ind];
			id /= binLen;
		}
		buf[--charPos] = r[(int) (id % binLen)];
		String str = new String(buf, charPos, (32 - charPos));
		// 不够长度的自动随机补全
		if (str.length() < s) {
			StringBuilder sb = new StringBuilder();
			sb.append(b);
			Random rnd = new Random();
			for (int i = 1; i < s - str.length(); i++) {
				sb.append(r[rnd.nextInt(binLen)]);
			}
			str += sb.toString();
		}
		return str;
	}
	
	/**
	 * 根据ID生成type类型的s位随机码
	 * @return 随机码
	 */
	public static String toSerialCode(long id, int s, String type) {
		char[] buf = new char[32];
		int charPos = 32;

		while ((id / binLen) > 0) {
			int ind = (int) (id % binLen);
			// System.out.println(num + "-->" + ind);
			buf[--charPos] = r[ind];
			id /= binLen;
		}
		buf[--charPos] = r[(int) (id % binLen)];
		String str = new String(buf, charPos, (32 - charPos));
		// 不够长度的自动随机补全
		if (str.length() < s) {
			StringBuilder sb = new StringBuilder();
			sb.append(b);
			Random rnd = new Random();
			for (int i = 1; i < s - str.length(); i++) {
				sb.append(r[rnd.nextInt(binLen)]);
			}
			str += sb.toString();
		}
		return type+str;
	}

	public static String makeInviteCode() {
		String inviteCode = "";

		return inviteCode;
	}



	/**
	 * 获取当前 jvm 的内存信息
	 *
	 * @return
	 */
	public static String toMemoryInfo() {

		Runtime currRuntime = Runtime.getRuntime();
		int nFreeMemory = (int) (currRuntime.freeMemory() / 1024 / 1024);
		int nTotalMemory = (int) (currRuntime.totalMemory() / 1024 / 1024);
		return nFreeMemory + "M/" + nTotalMemory + "M(free/total)";
	}
	/**
	 * 
	* Title: GenerateCouponCodeByBatch
	* Description:
	* @param tenantNum 租户id
	* @param couponId 优惠券模板id
	* @param channel 渠道
	* @return
	 */
	//生成一张优惠券
	public static String GenerateCouponCodeByOne(int tenantNum,int couponId,int channel){
		String couponNum="";
//		if(channel<10){
//			chnnelStr="0"+channel;
//		}else{
//			chnnelStr=channel+"";
//		}
		String couponIdStr="";
		if(couponId<100) {
			if(couponId<10) {
				couponIdStr="00"+couponId;
			}else {
				couponIdStr="0"+couponId;
			}
		}
		couponNum=intTo32(tenantNum)+couponIdStr+channel+RandomUtils.generateNumString(7);
		return couponNum;
		
	}
	
	public static List<String> generateCode(){
		List<String> list=new ArrayList<String>(9000000);
		int max=9000000;
		int min=1000000;
		//int k = 4;
		//生成0~n之间的数组
		int[] x = new int[max];
		for (int i = 0; i < max; i++) {
		    x[i] = min+i;
		}
		//开始随机n 个不重复数出来
		for (int i = 0; i < max; i++) {
			String coupon_code="";
		    Random random = new Random();
		   int t = random.nextInt(max - i) + i;
		   // 交换x[i]，x[t]的值
		   int temp = x[i];
		   x[i] = x[t];
		   x[t] = temp;
		   list.add(x[i]+"");
		}
		return list;
	}
	
	public static String intTo32(int num){  
		String result="";
        int array[]=new int[100];  
        int location=0;  
        while(num!=0){//当输入的数不为0时循环执行求余和赋值  
            int remainder=num%36;  
            num=num/36;  
            array[location]=remainder;//将结果加入到数组中去  
            location++;  
        } 
        for(int i=location-1;i>=0;i--){  
            if(array[i]>9){  
            	result=result+String.valueOf((char)(array[i]+55));
            } else {
            	result=result+array[i]+"";
            }
             
        }  
        if(result.length()<2)
        	result="0"+result;
        return result;
    }
	
}
